package com.turing.post.cardmanagement.util;

import javax.naming.InitialContext;

import com.turing.post.util.PCPSApplicationException;

public class AppCardOperationBusinessAgent {

	private static final AppCardOperationBusiness getAppCardOperationBusiness(
			AppManagement amg) throws PCPSApplicationException {
		try {
			InitialContext ctx = new InitialContext();
			AppCardOperationBusiness btb = (AppCardOperationBusiness) ctx
					.lookup(amg.getJndiName() + "#"
							+ AppCardOperationBusiness.class.getName());
			return btb;
		} catch (Exception ex) {
			throw new PCPSApplicationException("", "实现类未找到!");
		}
	}

	/**
	 * 产生读应用区域的APDU指令列表
	 */
	public static final ApduExecuteObj generateReadAppZoneApdu(AppManagement am)
			throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).generateReadAppZoneApdu(am);
	}

	/**
	 * 将APDU指令的结果进行解析,返回本应用特定的对象
	 * 
	 * @param am
	 * @return
	 */
	public static final AppZoneInf parseAppZoneObject(AppManagement am,
			ApduExecuteObj aeo) throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).parseAppZoneObject(am, aeo);
	}

	/**
	 * 将指定应用特定的对象进行HTML渲染,以驱动客户端展现
	 * 
	 * @param am
	 * @param appObj
	 * @return
	 * @throws PCPSApplicationException
	 */
	public static final String renderAppZoneObject(AppManagement am,
			AppZoneInf appObj) throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).renderAppZoneObject(am, appObj);
	}

	/**
	 * 产生圈存初始化APDU指令列表
	 * 
	 * @param am
	 * @param appObj
	 *            圈存前读出的卡内应用对象
	 * @param loadSeqNo
	 * @return
	 */
	public static final ApduExecuteObj generateLoadInitApdu(AppManagement am,
			AppZoneInf appObj, String loadSeqNo)
			throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).generateLoadInitApdu(am, appObj,
				loadSeqNo);
	}

	/**
	 * 解析圈存初始化结果,根据其结果产生圈存APDU指令列表
	 * 
	 * @param am
	 * @param aeo
	 * @return
	 */
	public static final ApduExecuteObj generateLoadApdu(AppManagement am,
			ApduExecuteObj aeo) throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).generateLoadApdu(am, aeo);
	}

	/**
	 * 解析圈存结果
	 * 
	 * @param am
	 * @param aeo
	 * @throws PCPSApplicationException
	 */
	public static final void parseLoadApduResult(AppManagement am, ApduExecuteObj aeo)
			throws PCPSApplicationException {
		getAppCardOperationBusiness(am).parseLoadApduResult(am, aeo);
	}

	/**
	 * 产生应用发行前卡片检查APDU指令列表
	 * 
	 * @param am
	 * @return
	 * @throws PCPSApplicationException
	 */
	public static final ApduExecuteObj generateAppPreIssueApdu(AppManagement am)
			throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).generateAppPreIssueApdu(am);
	}

	/**
	 * 解析发行前卡片检查APDU结果,成功后返回对象
	 * 
	 * @param am
	 * @param aeo
	 * @param cardSeqNo
	 * @return
	 * @throws PCPSApplicationException
	 */
	public static final AppZoneInf parseAppPreIssueApdu(AppManagement am,
			ApduExecuteObj aeo, String cardSeqNo)
			throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).parseAppPreIssueApdu(am, aeo,
				cardSeqNo);
	}

	/**
	 * 产生应用发行APDU指令列表
	 * 
	 * @param am
	 * @param azi
	 * @return
	 * @throws PCPSApplicationException
	 */
	public static final ApduExecuteObj generateAppIssueApdu(AppManagement am,
			AppZoneInf azi) throws PCPSApplicationException {
		return getAppCardOperationBusiness(am).generateAppIssueApdu(am, azi);
	}

	/**
	 * 解析应用发行APDU结果
	 * 
	 * @param am
	 * @param aeo
	 * @throws PCPSApplicationException
	 */
	public void parseAppIssueApduResult(AppManagement am, ApduExecuteObj aeo)
			throws PCPSApplicationException {
		getAppCardOperationBusiness(am).parseAppIssueApduResult(am, aeo);
	}
}
